window.popup = window.Popup = (function(window) {
    class Popup{
        constructor() {

        }
        confirm(text, cb = () => {}) {
            this.msgPrepare(text)
            this.box.style.height = '15%'
            let yesBtn = document.createElement('span')
            yesBtn.innerText = '确定'
            this.setStyle(yesBtn, {
                'position': 'absolute',
                'right': '70px',
                'bottom': '10px',
                'background-color': '#0ff',
                'color': '#fff',
                'border-radius': '2px',
                'padding': '2px 5px'
            })
            this.box.appendChild(yesBtn)

            let noBtn = document.createElement('span')
            noBtn.innerText = '取消'
            this.setStyle(noBtn, {
                'position': 'absolute',
                'right': '10px',
                'bottom': '10px',
                'background-color': '#f00',
                'color': '#fff',
                'border-radius': '2px',
                'padding': '2px 5px'
            })
            this.box.appendChild(noBtn)

            noBtn.onclick = e => this.close()
            yesBtn.onclick = e => {
                cb()
            }
        }
        close() {
            this.box.parentElement.removeChild(this.box)
        }
        msg(text, cb) {
            this.msgPrepare(text)
            setTimeout(() => {
                this.box.parentElement.removeChild(this.box)
                cb && cb()
            }, 2000)
        }
        msgPrepare(text) {
            this.box = document.createElement('div')
            this.box.innerText = text
            this.setStyle(this.box, {
                width: '50%',
                backgroundColor: 'rgba(255,255,0,.7)',
                color: '#f00',
                borderRadius: '4px',
                position: 'absolute',
                left: '50%',
                transform: 'translate(-50%,-50%)',
                padding: '10px',
                fontWeight: 'bold'
            })
            document.body.appendChild(this.box)
            let t = this.box.offsetHeight
            let maxT = document.documentElement.clientHeight / 2
            this.box.style.top = t + 'px'
            let timer = setInterval(() => {
                t += 10
                if(t >= maxT) {
                    t = maxT
                    clearInterval(timer)
                }
                this.box.style.top = t + 'px'
            }, 20)
        }
        setStyle(ele, styleObj) {
            for(var key in styleObj) {
                ele.style[key] = styleObj[key]
            }
        }
    }
    var popup 
    return (function(window) {
        if(!popup) popup = new Popup
        return popup
    })(window)
})(window)